package db

import (
	"time"
	"gopkg.in/mgo.v2"
	"github.com/astaxie/beego"

)



var mongoSession *mgo.Session
var database *mgo.Database

func GetMgo() *mgo.Session {
    return mongoSession
}

func GetDataBase() *mgo.Database {
    return database
}

func GetErrNotFound() error {
    return mgo.ErrNotFound
}


func init() {
	beego.Info("init db !!!")
	 beego.Info("host :"+beego.AppConfig.String("host"))
    dialInfo := &mgo.DialInfo{
        Addrs:     []string{beego.AppConfig.String("host")+":27027"}, //数据库地址 dbhost: mongodb://user@123456:127.0.0.1:27017
        Direct:    false,
        Timeout:   10 * time.Second,                  // 连接超时时间 timeout: 60 * time.Second
        Database:  "tcvb",
        // Source:    "tcvb",                   // 设置权限的数据库 authdb: admin
        Username:  "tcvb",                 // 设置的用户名 authuser: user
        Password:  "tcvb@spark",                // 设置的密码 authpass: 123456
        PoolLimit: 50,       // 连接池的数量 poollimit: 100
    }
	
    mongoSession, err := mgo.DialWithInfo(dialInfo)
    
    if err != nil {
        beego.Error(err)
    }

    mongoSession.SetMode(mgo.Monotonic, true)
    mongoSession.SetSocketTimeout(3 * time.Minute)
    
    //使用指定数据库
    database = mongoSession.DB("tcvb")
    beego.Info("init db ~~~")
}
